import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/Login'
import Home from '@/views/Home'

import TodoTask from '@/views/LoanBefore/todotask'
import DoneTask from '@/views/LoanBefore/donetask'
import Investigation from '@/views/LoanBefore/investigation'
import LoanApplication from '@/views/LoanBefore/loanapplication'
import CreditManage from '@/views/LoanBefore/creditmanage'

Vue.use(Router)
  let router = new Router({
    routes: [
      {
        path: '/',
        name: 'Home',
        component: Home
      },
      {
        path: '/login',
        name: 'Login',
        component: Login
      },
      {
        path:'/',
        component:Home,
        leaf:true,
        level:1,
        menuShow:true,
        children: [
          { path: '/LoanBefore/todotask', component: TodoTask, name: '待办任务' },
        ]
      },
      {
        path:'/',
        component:Home,
        leaf:true,
        level:1,
        menuShow:true,
        children: [
          { path: '/LoanBefore/donetask', component: DoneTask, name: '已办任务' },
        ]
      },
      {
        path:'/',
        component:Home,
        leaf:false,
        name:"贷前管理",
        level:1,
        menuShow:true,
        children: [
          { path: '/LoanBefore/investigation', component: Investigation, name: '贷前调查' },
          { path: '/LoanBefore/loanapplication', component: LoanApplication, name: '授信管理' },
          { path: '/LoanBefore/creditmanage', component: CreditManage, name: '贷款申请' },
        ]
      },
      {
        path:'/',
        component:Home,
        leaf:false,
        name:"贷中管理",
        level:1,
        menuShow:true,
        children: [
          { path: '/LoanBefore/investigation', component: Investigation, name: '合同管理' },
          { path: '/LoanBefore/loanapplication', component: LoanApplication, name: '贷款管理' }
        ]
      },
      {
        path:'/',
        component:Home,
        leaf:false,
        name:"贷后管理",
        level:1,
        menuShow:true,
        children: [
          { path: '/LoanBefore/investigation', component: Investigation, name: '还款管理' },
          { path: '/LoanBefore/loanapplication', component: LoanApplication, name: '贷后检查' }
        ]
      },
      {
        path:'/',
        component:Home,
        leaf:false,
        name:"催收管理",
        level:1,
        menuShow:true,
        children: [
          { path: '/LoanBefore/investigation', component: Investigation, name: '案件管理' },
          { path: '/LoanBefore/loanapplication', component: LoanApplication, name: '催收管理' }
        ]
      }
    ]
  })
  router.beforeEach((to, from, next) => {
    if (to.path.startsWith('/login')) {
      window.sessionStorage.removeItem('access-user')
      next()
    } else {
      let user = JSON.parse(window.sessionStorage.getItem('access-user'))
      if (!user) {
        next({ path: '/login' })
      } else {
        next()
      }
    }
  })
  export default router
